package com.example.mybatisplusdemo.web.controller;

import com.example.mybatisplusdemo.common.JsonResponse;
import com.example.mybatisplusdemo.model.domain.BookingTrack;
import com.example.mybatisplusdemo.service.BookingTrackService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/booking")
@RequiredArgsConstructor
public class BookingRedirectController {

    private final BookingTrackService bookingTrackService;

    /**
            * 跳转到供应商预订页面
     */
    @PostMapping("/redirect")
    public JsonResponse<Object> redirectToSupplier(
            @RequestBody BookingTrack request) {
        try {
            // 1. 验证请求参数
            if (request == null || request.getFlightId() == null || request.getUserId() == null || request.getSupplier() == null) {
                return JsonResponse.failure("请求参数不完整").setCode(400);
            }

            // 2. 生成跟踪ID
            String trackingId = bookingTrackService.createTracking(
                    request.getFlightId(),
                    request.getUserId(),
                    request.getSupplier()
            );

            // 3. 生成跳转链接
            String redirectUrl = bookingTrackService.generateRedirectUrl(
                    request.getFlightId(),
                    request.getSupplier(),
                    trackingId
            );

            return JsonResponse.success(redirectUrl, "跳转链接生成成功");
        } catch (Exception e) {
            return JsonResponse.failure("跳转失败: " + e.getMessage())
                    .setCode(500);
        }
    }
}